package com.atguigu.dga.governance.service.impl;

import com.alibaba.druid.support.json.JSONUtils;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.dga.governance.bean.GovernanceAssessTecOwner;
import com.atguigu.dga.governance.mapper.GovernanceAssessTecOwnerMapper;
import com.atguigu.dga.governance.service.GovernanceAssessTecOwnerService;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 技术负责人治理考评表 服务实现类
 * </p>
 *
 * @author wuzhida
 * @since 2024-07-02
 */
@Service
@DS("dga")
public class GovernanceAssessTecOwnerServiceImpl extends ServiceImpl<GovernanceAssessTecOwnerMapper, GovernanceAssessTecOwner> implements GovernanceAssessTecOwnerService {


    /**
     * 计算人级分数
     * @param assessDate
     */
    @Override
    public void calcTecOwnerScore(String assessDate) {
        remove(
                new QueryWrapper<GovernanceAssessTecOwner>()
                        .eq("assess_date", assessDate)
        );
        
        List<GovernanceAssessTecOwner> tecOwnerScoreList = getBaseMapper().getTecOwnerScore(assessDate);
        
        saveBatch(tecOwnerScoreList);
    }

    /**
     * 分组人员排行榜
     * 返回值 
     *      [{"tecOwner":"zhang3" ,"score":99},
     *      {"tecOwner":"li4" ,"score":98},
     *      {"tecOwner": "wang5","score":97}   ]
     * @return
     */
    @Override
    public String getRankList() {
        List<Map<String, Object>> list = listMaps(
                new QueryWrapper<GovernanceAssessTecOwner>()
                        .select("tec_owner AS tecOwner", "score")
                        .inSql("assess_date", "SELECT MAX(assess_date) FROM governance_assess_tec_owner")
                        .orderByDesc("score")
        );
        return JSONObject.toJSONString(list);
    }
}
